/**
 * @author benny.zheng
 * @data 2016-06-06
 * @description 本文件用于方便复制粘贴UI模块之用，请更新这里的说明
 *              另外，考虑到一般是放在js/src/pages/page-name/main.js，因此使用../../
 *              如果不是这个目录，请更改成正确的相对路径
 */
//----------------require--------------

var viewport = require("mlib/dom/viewport"); // viewport
var base = require("mlib/comp/base"); // 基础对象
var parseModule = require("mlib/dom/parseModule"); // 页面模块自动解析
var getMeta = require("../plugin/dom/meta");
var wechat = require("../plugin/wx/wx-api");

var closest = require("mlib/dom/closest");
var ajax = require("mlib/io/ajax");
var virtualLink = require("mlib/util/virtualLink");
var lazyload = require("mlib/dom/lazyload");
var Dialog = require("../layer/dialog");
var Countdown = require("../plugin/util/count-time-down");
var touch = require("mlib/evt/touch");

var statusRender = require("./viewstatus.ejs");


module.exports = function(node, opts) {
    //-----------声明模块全局变量-------------
    var nodeList = null; // 存储所有关键节点
    var that = base();
    var data = null;
	var metadata =getMeta();
	
	var voucherobj = {};
	
    var status_content = null;
    var m_dialog = null;
    var m_container = null;
    //-------------事件响应声明---------------
    var evtFuncs = {
		showStatus:function(evt){
			var target =closest(evt.target,".m-view-status",true);
			var nocode = target.getAttribute("data-no");
			status_content = statusRender(voucherobj[nocode]);
			m_dialog = Dialog({
				title:"查看状态",
				content:status_content
			});
			m_dialog.bind("aftershow",function(layer){

				var contentNode = layer.data.nodeList.content;
				m_container = closest(contentNode,".m-dialog",true);
				var mask = document.querySelector(".m-mask");
					
					mask.style.position = 'fixed';
				
			});
			m_dialog.init();
			m_container.style.position = 'fixed';
		},
		soonpay:function(){
			if(nodeList.soonpay.classList.contains("disabled")){
				return;
			}
			document.querySelector("#orderForm").submit();
		}
	}

    //-------------子模块实例化---------------
    var initMod = function() {
			 custFuncs.voucherData(data.voucherData);
			 virtualLink(function(evt){
				location.href = evt.target.getAttribute("data-href");
			 });
			 if(data.cancelExpireTime!=""){
				new Countdown(document.getElementById("cancel-date"),{
					"format": "hh小时mm分ss秒" | "hh:mm:ss", 
					"lastTime": data.cancelExpireTime,
					"callback":function(){},
					"complete":function(box){
						var pbox = closest(box,".countdown");
						nodeList.soonpay.classList.add("disabled");
						if (pbox.parentNode) {
						  pbox.parentNode.removeChild(pbox);
						}
					},
				});
			 }
             wechat('off').init();
    }

    //-------------绑定事件------------------
    var bindEvents = function() {
         nodeList.viewdetail && touch.on(nodeList.viewdetail,"tap",evtFuncs.showStatus);
         nodeList.soonpay && touch.on(nodeList.soonpay,"tap",evtFuncs.soonpay);
	}

    //-------------自定义函数----------------
    var custFuncs = {
        voucherData:function(data){
			data.forEach(function(item){
				if(item.childitems){
					item.childitems.forEach(function(v,k){
						voucherobj[v.voucherid] = {
							"maxusetime":v.maxusetime, 
							"usednumber":v.usednumber, 
							"invalidnumber":v.invalidnumber, 
							"freezenumber":v.freezenumber, 
							"expirenumber":v.expirenumber 
						};
					});
				}else{
					voucherobj[item.voucherid] = {
						"maxusetime":item.maxusetime, // 待使用
						"usednumber":item.usednumber, // 已使用
						"invalidnumber":item.invalidnumber, // 退订成功
						"freezenumber":item.freezenumber, // 退订中
						"expirenumber":item.expirenumber // 已过期
					};
				}
			});
		}
    }

    //-------------一切从这开始--------------
    var init = function(_data) {

        data = _data;
        // 根据数据初始化模块
        // opts["render"]({ "title": data["title"] });

        // 找到所有带有node-name的节点
        nodeList = parseModule(node);

        // 子模块实例化
        initMod();
        // 绑定事件
        bindEvents();


    }

    //---------------暴露API----------------
    that.init = init;

    return that;
};